# led操作
import time
from machine import Pin
from machine import Timer


def of(index=0):
    return ZZ_LED(index)


class ZZ_LED():
    def __init__(self, index=0):
        # 定时器0
        self.timer0 = Timer(index)
        self.blue_led = Pin(2, Pin.OUT)

    def get_led_blue(self):
        return self.blue_led

    # led蓝灯操作
    def led_blue(self, open):
        # print(f'led_blue: {open}')
        led = self.get_led_blue()
        if open:
            return led.value(1)
        else:
            return led.value(0)

    # led红灯开始闪
    def led_dodge(self, ms=500):
        time.sleep_ms(100)
        self.led_dodge_stop()
        led = self.get_led_blue()
        led_callback = lambda t: self.led_blue(not led.value())
        self.timer0.init(period=ms, mode=Timer.PERIODIC, callback=led_callback)

    # led红灯停止闪
    def led_dodge_stop(self, led_open=False):
        self.timer0.deinit()
        self.led_blue(led_open)
